<template>
  <div class="signup-page mx-auto p-3 w-330">
    <h5>注册账号</h5>
    <ValidateForm @form-submit="onFromSubmit">
      <div class="mb-3">
        <label class="form-lable">邮箱地址</label>
        <ValidateInpt
          typr="text"
          placeholder="请输入正确的邮箱"
          v-model="formData.email"
          :rules="emailRules"
          @value="emailVal"
        >
        </ValidateInpt>
      </div>

      <div class="mb-3">
        <label class="form-lable">昵称</label>
        <ValidateInpt
          typr="text"
          placeholder="请输入昵称"
          v-model="formData.nickName"
          :rules="nameRules"
          @value="nameVal"
        >
        </ValidateInpt>
      </div>

      <div class="mb-3">
        <label class="form-lable">密码</label>
        <ValidateInpt
          typr="text"
          placeholder="请输入密码"
          v-model="formData.password"
          :rules="passwordRules"
          @value="passwordVal"
        >
        </ValidateInpt>
      </div>

      <div class="mb-3">
        <label class="form-lable">重复密码</label>
        <ValidateInpt
          typr="text"
          placeholder="请再次输入密码"
          v-model="formData.repeatPassword"
          :rules="repeatPasswordRules"
          @value="repeatPasswordVal"
        >
        </ValidateInpt>
      </div>

      <template #submit>
        <button type="button" class="btn btn-primary btn-block btn-large">
          注册账号
        </button>
      </template>
    </ValidateForm>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import ValidateForm from "@/components/validateForm.vue";
import ValidateInpt, { RulesProp } from "@/components/ValidateInpt.vue";
import createMessage from "@/hooks/createMessage";
import { useRouter } from "vue-router";
// import { useStore } from "vuex";
import axios from "axios";
export default defineComponent({
  components: {
    ValidateInpt,
    ValidateForm,
  },
  setup() {
    const formData = reactive({
      email: "",
      nickName: "",
      password: "",
      repeatPassword: "",
    });
    const emailVal = (val: string) => {
      formData.email = val;
    };
    const nameVal = (val: string) => {
      formData.nickName = val;
    };
    const passwordVal = (val: string) => {
      formData.password = val;
    };
    const repeatPasswordVal = (val: string) => {
      formData.repeatPassword = val;
    };
    const router = useRouter();
    const emailRules: RulesProp = [
      //这个是邮箱的规则，应该被这个东西约束
      {
        type: "required",
        message: "电子邮箱地址不能为空",
      },
      {
        type: "email",
        message: "请输入正确的电子邮箱",
      },
    ];

    const nameRules: RulesProp = [
      //这个是邮箱的规则，应该被这个东西约束
      {
        type: "required",
        message: "昵称不能为空",
      },
    ];
    const passwordRules: RulesProp = [
      {
        type: "required",
        message: "密码不能为空",
      },
    ];

    const repeatPasswordRules: RulesProp = [
      {
        type: "required",
        message: "重复密码不能为空",
      },
      {
        type: "custom",
        validator: () => {
          return formData.password === formData.repeatPassword;
        },
        message: "密码不相同",
      },
    ];
    const onFromSubmit = (result: boolean) => {
      if (result) {
        const payload = {
          email: formData.email,
          nickName: formData.nickName,
          password: formData.password,
        };
        axios
          .post("/users", payload)
          .then((data) => {
            console.log(data);
            createMessage("注册成功，正在跳转登录页面", "success", 2000);
            setTimeout(() => {
              router.push("/login");
            }, 2000);
          })
          .catch((e) => {
            console.log(e);
          });
      }
    };

    return {
      formData,
      emailRules,
      nameRules,
      passwordRules,
      repeatPasswordRules,
      onFromSubmit,
      emailVal,
      nameVal,
      passwordVal,
      repeatPasswordVal,
    };
  },
});
</script>

<style>
.w-330 {
  max-width: 330px;
}
</style>
